package DomainObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.omg.PortableServer.POA;

import Combat5.Server;
import ca.etsmtl.log720.lab1.BanqueInfractionsPOA;
import ca.etsmtl.log720.lab1.CollectionInfraction;
import ca.etsmtl.log720.lab1.CollectionInfractionHelper;
import ca.etsmtl.log720.lab1.Dossier;
import ca.etsmtl.log720.lab1.Infraction;
import ca.etsmtl.log720.lab1.InfractionHelper;
import ca.etsmtl.log720.lab1.NiveauHorsBornesException;
//create by martin souliere
public class BanqueInfractionsImpl extends BanqueInfractionsPOA {

	private CollectionInfractionImpl _infractions;
	public BanqueInfractionsImpl() throws ClassNotFoundException, IOException{
		try {
			FileInputStream fileInputStream = new FileInputStream(
					"Infractions.bin");
			ObjectInputStream objInputStream = new ObjectInputStream(
					fileInputStream);
			_infractions = (CollectionInfractionImpl) objInputStream
					.readObject();
			objInputStream.close();
		} catch (FileNotFoundException e) {
			_infractions = new CollectionInfractionImpl();
		}
	}
	public CollectionInfraction infractions() {
		try {
			// Recuperer le POA cree dans le serveur
			POA rootpoa = Server._poa;

			// Activer l'objet et retourne l'objet CORBA
			org.omg.CORBA.Object obj = rootpoa
					.servant_to_reference(_infractions);

			// Retourner une Collection d'infractions
			return CollectionInfractionHelper.narrow(obj);
			
		} catch (Exception e) {
			System.out.println("Erreur retour de l'objet CollectionEtudiant : "	+ e);
			return null;
		}
	}

	public CollectionInfraction trouverInfractionsParDossier(Dossier mydossier) {
		try {
			// Recuperer le POA cree dans le serveur
			POA rootpoa = Server._poa;

			// Activer l'objet et retourne l'objet CORBA
			org.omg.CORBA.Object obj = rootpoa
					.servant_to_reference(new CollectionInfractionImpl(mydossier.getListeInfraction(), _infractions.getListeInfractions()));

			// Retourner une Collection d'infractions
			return CollectionInfractionHelper.narrow(obj);
			
		} catch (Exception e) {
			System.out.println("Erreur retour de l'objet CollectionEtudiant : "	+ e);
			return null;
		}
	}

	public Infraction trouverInfractionParId(int idInfraction) { 
		InfractionImpl infraction = null;
		Infraction current = null;
		for (int i = 0; i < _infractions.size(); i++) {
			current = _infractions.getInfraction(i);
			if(current.id() == idInfraction)
			{
				infraction = new InfractionImpl(current);
				break;
			}
		}
		try {
			// Recuperer le POA cree dans le serveur
			POA rootpoa = Server._poa;

			// Activer l'objet et retourne l'objet CORBA
			org.omg.CORBA.Object obj = rootpoa.servant_to_reference(infraction);

			// Retourner une infraction
			return InfractionHelper.narrow(obj);
		} catch (Exception e) {
			return null;
		}
	}

	public void ajouterInfraction(String description, int niveau)
			throws NiveauHorsBornesException {
		if(niveau < 1 || niveau > 10 )
				throw new NiveauHorsBornesException();
		InfractionImpl infraction = new InfractionImpl(_infractions.size() + 1,description, niveau);
		_infractions.getListeInfractions().add(infraction);
		save();	
	}
	///sauvegarde la collection dans une fichier bin pour remplacer la base de données
	private void save()
	{
		try {
			File file = new File("Infractions.bin");
			if(!file.exists()) {
				file.createNewFile();
			}  
			else
			{
				file.delete();
				file.createNewFile();
			}
			FileOutputStream fileOutPutStream = new FileOutputStream("Infractions.bin");
			ObjectOutputStream objOutputStream = new ObjectOutputStream(fileOutPutStream);
			objOutputStream.writeObject(_infractions);
			objOutputStream.flush();
			objOutputStream.close();
		} catch (Exception e) {
			
		}	
	}
}
